require("GlobalConfig")
cc.Class({
    extends: cc.Component,

    properties: {
        Fy_NodeHall:cc.Node,
        Fy_GuessDialect:cc.Node,
    },
    onLoad:function(){
        UserData.Fy_GuessDialect = this.Fy_GuessDialect;
        this.onGetUserInfo();
        this.showGameClub();
    },
    onGetUserInfo:function(){
        var self = this;
        if (cc.sys.platform === cc.sys.WECHAT_GAME){
            wx.login({
                success: function (data) {
                    wx.getSetting({ //获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。
                        success (res){
                            if (res.authSetting['scope.userInfo']) {
                                wx.getUserInfo({
                                    success: function (res) {
                                        UserData.UserInfo = res.userInfo;
                                        UserData.UserInfoType = true; 
                                        UserData.UserId = data.code;   
                                        self.onSetLoginBtn() 
                                    }
                                });
                            }else{
                                UserData.UserInfoType = false; 
                                self.onSetUserInfoBtn(data.code) 
                            }
                        }
                    });
                },
                fail:function () {
                    wx.showModal({
                        title: '提示',
                        content: '请检查网络状态',
                        showCancel:false
                    })
                },
            });
            wx.onShow(function(){ // 回到前台方法
                console.log("回到前台")
            });
            wx.getSystemInfo({    // 获取系统信息
                success(res){
                    UserData.Height = res.windowHeight;
                    UserData.Width =res.windowWidth;
                }
            });
        };
    },
    onSetUserInfoBtn:function(code){
        var Mtop = UserData.Height ? UserData.Height - 180: 460;
        var Mleft =  UserData.Width ? UserData.Width/2 - 92: 95;
        var self = this;
        var UserButton = wx.createUserInfoButton({ // 创建用户信息按钮
            type: 'image',
            image: 'https://lg-0xdei6bu-1253760414.cos.ap-shanghai.myqcloud.com/PicStarPingPang.png',
            style: {
                left: Mleft,
                top: Mtop,
                width: 184,
                height: 72,
                lineHeight:72
            }
        });
        UserButton.onTap((res) => {              
            if(!UserData.UserInfoType && res.userInfo){
                UserData.UserInfo = res.userInfo;
                UserData.UserInfoType = true;   
                UserData.UserId = code;   
                UserButton.destroy();
                self.onSetLoginBtn()    
            }                
        })
    },
    onSetLoginBtn(){ // 授权
        wx.cloud.init({
            traceUser: true,
            env: 'testtest-98875e'
        });
        var sefl = this;
        wx.cloud.callFunction({
            name: 'getopenid',
            complete(res){
                UserData.UserInfo.openId = res.result._id;
                UserData.UserInfo.answer = res.result.answer;
                UserData.UserInfo.registrationTime = res.result.registrationTime;
                UserData.UserInfo.works = res.result.works;
                // sefl.Fy_NodeHall.getComponent("NodeHall").onOwnInformation();
                ddz.onLoadingData.onMyWork();
                ddz.onLoadingData.onMyAnswer();
            }
        });
        console.log("授权通过");
        console.log(UserData);
        this.node.active = false;
        this.Fy_NodeHall.active = true;
    },
    showGameClub() {
        window.GameClubButton = wx.createGameClubButton({
            type:"image",
            style: {
                lineHeight:1,
                left: 15,
                top: 55,
                width: 40,
                height: 40
            },
            icon:"light"
        });
        window.GameClubButton.show()
    }
});
